package Webpage;

import AutoTest.AutoFun;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-09-09
 * Time: 16:10
 */
//测试注册功能
public class Register {
    private WebDriver driver=null;
    private String url="http://8.155.1.153/?s=user/regInfo.html";
    private Scanner scanner=new Scanner(System.in);
    private String str=null;
    private String name=null;
    public void test(){
        driver= AutoFun.getDriver(url);
        test1();//未勾选同意协议，其余正确
        System.out.println("test1执行完成");
        test2();//用户名格式错误，其余正确
        System.out.println("test2执行完成");
        test3();//密码格式错误，其余正确
        System.out.println("test3执行完成");
        test4();//验证码错误，其余正确
        System.out.println("test4执行完成");
        test5();//正确注册
        System.out.println("test5执行完成");
        System.out.println("注册功能测试完成");
    }
    private void test1(){
        SimpleDateFormat sim=new SimpleDateFormat("yymmdd_HHmmssSS");
        Date time=new Date();
        name=sim.format(time);
        str=scanner.next();
        //未勾选用户协议，其余正确
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[1]/input")).sendKeys(name);
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[2]/input")).sendKeys("123456");
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[3]/input")).sendKeys(str);
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[5]/button")).click();
    }
    private void test2(){
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[4]/label")).click();
        //未填写用户名
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[1]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[5]/button")).click();
        //用户名长度<2
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[1]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[1]/input")).sendKeys("1");
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[5]/button")).click();
        //用户名长度>18
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[1]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[1]/input")).sendKeys("1");
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[5]/button")).click();
    };
    private void test3(){
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[1]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[1]/input")).sendKeys(name);
        //未填写密码
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[5]/button")).click();
        //登陆密码长度<6
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[2]/input")).sendKeys("123");
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[5]/button")).click();
        //登陆密码长度>18
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[2]/input")).sendKeys("123456789123456789123");
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[5]/button")).click();
    };
    private void test4(){
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[2]/input")).sendKeys("123456");
        //未填写验证码
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[3]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[5]/button")).click();
        //验证码错误
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[3]/input")).sendKeys("qqqq");
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[5]/button")).click();
    };
    private void test5(){
        //正确注册
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[3]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[3]/input")).sendKeys(str);
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]/form/div[5]/button")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[4]/div[3]/div/div[1]/div[1]/div[1]/div"));
        driver.close();
    };

}
